local extension = Package("fengqi_wind")
extension.extensionName = "aaa_fengqi"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["fengqi_wind"] = "风",
}


local weiyan = General(extension, "efengqi__weiyan", "han", 4)

local kuanggu = fk.CreateTriggerSkill{
  name = "efengqi__kuanggu",
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and (data.extra_data or {}).kuanggucheck
  end,
  on_trigger = function(self, event, target, player, data)
    for i = 1, data.damage do
      if i > 1 and (self.cost_data == "Cancel" or not player:hasSkill(self)) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {"efengqi__kuanggu_draw", "Cancel"}
    if player:canUseTo(Fk:cloneCard("old__drugs"), player) then
      table.insert(choices, 2, "efengqi__kuanggu_old")
    end
    self.cost_data = room:askForChoice(player, choices, self.name)
    return self.cost_data ~= "Cancel"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == "efengqi__kuanggu_draw" then
      player:drawCards(1, self.name)
      if not player.dead then
        room:useVirtualCard("drugs", nil, player, player, self.name, true)
      end
    else
      room:recover { num = 1, skillName = self.name, who = player, recoverBy = player }
      if not player.dead then
        room:useVirtualCard("old__drugs", nil, player, player, self.name, true)
      end
    end
  end,

  refresh_events = {fk.BeforeHpChanged},
  can_refresh = function(self, event, target, player, data)
    if data.damageEvent and player == data.damageEvent.from and player:distanceTo(target) < 2 and not target:isRemoved() then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    data.damageEvent.extra_data = data.damageEvent.extra_data or {}
    data.damageEvent.extra_data.kuanggucheck = true
  end,
}
weiyan:addSkill(kuanggu)

local qimou = fk.CreateActiveSkill{
  name = "efengqi__qimou",
  anim_type = "offensive",
  card_num = 0,
  target_num = 0,
  frequency = Skill.Limited,
  prompt = "#efengqi__qimou",
  interaction = function()
    return UI.Spin {
      from = 1,
      to = Self.hp,
    }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and player.hp > 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local tolose = self.interaction.data
    room:loseHp(player, tolose, self.name)
    if player.dead then return end
    room:addPlayerMark(player, "@qimou-turn", tolose)
    room:addPlayerMark(player, MarkEnum.SlashResidue.."-phase", tolose)
  end,
}
local qimou_distance = fk.CreateDistanceSkill{
  name = "#efengqi__qimou_distance",
  correct_func = function(self, from, to)
    return -from:getMark("@qimou-turn")
  end,
}
qimou:addRelatedSkill(qimou_distance)
weiyan:addSkill(qimou)

Fk:loadTranslationTable{
  ["efengqi__weiyan"] = "魏延", -- FM 002
  ["#efengqi__weiyan"] = "气贯云汉",
  ["designer:efengqi__weiyan"] = "Romantic",
  ["illustrator:efengqi__weiyan"] = "",

  ["efengqi__kuanggu"] = "狂骨",
  [":efengqi__kuanggu"] = "当你对距离1以内的角色造成1点伤害后，你可以选择：1.摸一张牌并视为使用一张【散】；2.回复1点体力并视为使用一张老【散】。",
  ["efengqi__kuanggu_draw"] = "摸一张牌，视为使用【散】",
  ["efengqi__kuanggu_old"] = "视为使用一张老【散】",

  ["efengqi__qimou"] = "奇谋",
  [":efengqi__qimou"] = "限定技，出牌阶段，你可以失去任意点体力，然后本回合你计算与其他角色的距离-X且出【杀】次数+X。（X为你以此法失去的体力数）",
  ["#efengqi__qimou"] = "奇谋：失去X点体力，与其他角色的距离-X，出【杀】数+X",

  ["$efengqi__kuanggu1"] = "反骨狂傲，彰显本色！",
  ["$efengqi__kuanggu2"] = "只有战场，能让我感到兴奋！",
  ["$efengqi__qimou1"] = "为了胜利，可以出其不意！",
  ["$efengqi__qimou2"] = "勇战不如奇谋。",
  ["~efengqi__weiyan"] = "这次失败，意料之中……",
}


local xiaoqiao = General(extension, "efengqi__xiaoqiao", "wu", 3, 3, General.Female)
local efengqi__shaoyan = fk.CreateTriggerSkill{
  name = "efengqi__shaoyan",
  anim_type = "control",
  events = {fk.TargetConfirmed, fk.AskForRetrial},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.AskForRetrial then
      return not player:isNude()
    else
      return target == player and data.from and player.room:getPlayerById(data.from):getHandcardNum() > player:getHandcardNum()
      and player:getMark("efengqi__shaoyan-round") == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.AskForRetrial then
      local ids = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id):getMark("@@efengqi__qiqin") ~= 0 end)
      local cards = player.room:askForCard(player, 1, 1, true, self.name, true, tostring(Exppattern{ id = ids }),
      "#efengqi__shaoyan-ask::"..target.id..":"..data.reason..":"..data.card:toLogString())
      if #cards > 0 then
        self.cost_data = {cards = cards, tos = {target.id}}
        return true
      end
    else
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AskForRetrial then
      room:retrial(Fk:getCardById(self.cost_data.cards[1]), player, data, self.name, true)
    else
      room:setPlayerMark(player, "efengqi__shaoyan-round", 1)
      local to = room:getPlayerById(data.from)
      local cards = room:askForCard(to, 1, 1, false, self.name, false, ".|.|.|hand", "#efengqi__shaoyan-card")
      room:setCardMark(Fk:getCardById(cards[1]), "@@efengqi__qiqin", 1)
      local record = room:getBanner("efengqi__qiqin_record") or {}
      table.insertIfNeed(record, cards[1])
      room:setBanner("efengqi__qiqin_record", record)
    end
  end,
}
xiaoqiao:addSkill("efengqi__qiqin")
xiaoqiao:addSkill(efengqi__shaoyan)

Fk:loadTranslationTable{
  ["efengqi__xiaoqiao"] = "小乔",
  ["#efengqi__xiaoqiao"] = "",
  ["designer:efengqi__xiaoqiao"] = "Romantic",
  ["illustrator:efengqi__xiaoqiao"] = "",

  ["efengqi__shaoyan"] = "韶颜",
  [":efengqi__shaoyan"] = "当一张判定牌生效前，你可以用一张“琴”替换之；每轮限一次，当你成为手牌数大于你的角色使用牌的目标后，其须将一张手牌标记为“琴”。",
  ["#efengqi__shaoyan-card"] = "韶颜：你须将一张手牌标记为“琴”",
  ["#efengqi__shaoyan-ask"] = "韶颜：你可以用一张“琴”替换 %dest “%arg”的判定牌%arg2",
}

local caoren = General(extension, "efengqi__caoren", "wei", 4)
Fk:loadTranslationTable{
  ["efengqi__caoren"] = "曹仁",
  ["#efengqi__caoren"] = "天将临城",
  ["illustrator:efengqi__caoren"] = "Ccat",
  ["designer:efengqi__caoren"] = "zzcclll朱苦力",
  ["~efengqi__caoren"] = "实在是守不住了……",
}

local jushou = fk.CreateTriggerSkill{
  name = "efengqi__jushou",
  anim_type = "defensive",
  events = {fk.EventPhaseEnd, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.EventPhaseEnd and player.phase ~= Player.Play then return false end
      return #player.room:canMoveCardInBoard() > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askForChooseToMoveCardInBoard(player, "#efengqi__jushou-move", self.name, true)
    if #tos > 0 then
      self.cost_data = { tos = tos }
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tar1, tar2 = room:getPlayerById(self.cost_data.tos[1]), room:getPlayerById(self.cost_data.tos[2])
    room:askForMoveCardInBoard(player, tar1, tar2, self.name)
    if player:isAlive() then
      player:turnOver()
    end
    room:endTurn()
  end,
}
caoren:addSkill(jushou)

local jiewei = fk.CreateViewAsSkill{
  name = "efengqi__jiewei",
  anim_type = "control",
  pattern = "nullification",
  prompt = "#efengqi__jiewei-vs",
  handly_pile = true,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("nullification")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function (self, player, response)
    return not response and not player.faceup and
    (#player:getCardIds("e") > 0 or #player:getHandlyIds() > 0)
  end,
}

local jiewei_trigger = fk.CreateTriggerSkill{
  name = "#efengqi__jiewei_trigger",
  mute = true,
  events = {fk.TurnedOver},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(jiewei) and player.faceup
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(jiewei.name)
    room:notifySkillInvoked(player, jiewei.name, "drawcard")
    player:drawCards(3, jiewei.name)
  end,
}
jiewei:addRelatedSkill(jiewei_trigger)
caoren:addSkill(jiewei)

Fk:loadTranslationTable{
  ["efengqi__jushou"] = "据守",
  [":efengqi__jushou"] = "出牌阶段结束时，或你受到伤害后，你可以移动场上一张牌，然后翻面并结束当前回合。",
  ["#efengqi__jushou-move"] = "据守：选择两名角色，移动其场上一张牌，然后翻面并结束回合",

  ["efengqi__jiewei"] = "解围",
  [":efengqi__jiewei"] = "若你武将牌背面朝上，你可以将装备牌当【无懈可击】使用，且翻至正面后摸三张牌。",
  ["#efengqi__jiewei-vs"] = "解围：你可以将装备牌当【无懈可击】使用",
  ["#efengqi__jiewei_trigger"] = "解围",

  ["$efengqi__jushou1"] = "坚守勿出，严阵以待。",
  ["$efengqi__jushou2"] = "以静制动，以逸待劳。",
  ["$efengqi__jiewei1"] = "以守为攻，伺机而动！",
  ["$efengqi__jiewei2"] = "援军已到，转守为攻！",
}

local godlvmeng = General(extension, "efengqi__godlvmeng", "god", 4)

local gongxin = fk.CreateTriggerSkill{
  name = "efengqi__gongxin",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player.phase == Player.Draw
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room:getOtherPlayers(player), function (p) return not p:isKongcheng() end)
    if #tos == 0 then return false end
    tos = table.map(tos, Util.IdMapper)
    local to = room:askForChoosePlayers(player, tos, 1, 1, "#efengqi__gongxin-ivk", self.name, true)
    if to and #to == 1 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    if to.dead and event == fk.EventPhaseStart then
      room:setPlayerMark(player, self.name, 1)
    else
      local hearts = table.filter(to:getCardIds("h"), function (id)
        return Fk:getCardById(id).suit == Card.Heart
      end)
      local ids, choice = U.askforChooseCardsAndChoice(player, hearts, {"#efengqi__gongxin-place"}, self.name, "efengqi__gongxin", {"#efengqi__gongxin-cancel"}, 1, 1, to:getCardIds("h"))
      if choice == "#efengqi__gongxin-cancel" and event == fk.EventPhaseStart then
        room:setPlayerMark(player, self.name, 1)
      elseif ids and #ids == 1 then
        room:moveCardTo(ids, Card.DrawPile, nil, fk.ReasonPut, self.name, nil, true)
      end
    end
  end
}

local gongxin_delay = fk.CreateTriggerSkill{
  name = "#efengqi__gongxin_delay",
  anim_type = "control",
  events = {fk.EventPhaseEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(gongxin) and player.phase == Player.Draw and player:getMark(gongxin.name) == 1
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, gongxin.name, 0)
    gongxin:doCost(event, target, player, data)
  end,
}
gongxin:addRelatedSkill(gongxin_delay)

Fk:addPoxiMethod{
  name = "efengqi__shelie",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local suit = Fk:getCardById(to_select).suit
    return table.every(data[2], function (id)
      return Fk:getCardById(id).suit ~= suit
    end)
  end,
  feasible = function(selected)
    return true
  end,
}

local shelie = fk.CreateTriggerSkill{
  name = "efengqi__shelie",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player.phase == Player.Play
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(5)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonPut,
      skillName = self.name,
      proposer = player.id
    })
    if player.dead then return end
    local choice = room:askForChoice(player, {"#efengqi__shelie-obtain", "#efengqi__shelie-cancel"}, self.name)
    if choice == "#efengqi__shelie-cancel" and event == fk.EventPhaseStart then
      room:setPlayerMark(player, self.name, 1)
    else
      local get = {}
      for _, id in ipairs(cards) do
        local suit = Fk:getCardById(id).suit
        if table.every(get, function (id2)
          return Fk:getCardById(id2).suit ~= suit
        end) then
          table.insert(get, id)
        end
      end
      get = room:askForArrangeCards(player, self.name, cards, "#shelie-choose", false, 0, {5, 4}, {0, #get}, ".", self.name, {{}, get})[2]
      if #get > 0 then
        room:obtainCard(player, get, true, fk.ReasonPrey)
      end
      cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
      if #cards > 0 then
        room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJustMove, self.name)
      end
    end
  end
}

local shelie_delay = fk.CreateTriggerSkill{
  name = "#efengqi__shelie_delay",
  anim_type = "control",
  events = {fk.TurnEnd},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(shelie) and player == target and player:getMark(shelie.name) == 1
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, shelie.name, 0)
    shelie:doCost(event, target, player, data)
  end,
}
shelie:addRelatedSkill(shelie_delay)

godlvmeng:addSkill(gongxin)
godlvmeng:addSkill(shelie)

Fk:loadTranslationTable{
  ["efengqi__godlvmeng"] = "神吕蒙",
  ["designer:efengqi__godlvmeng"] = "zzcclll朱苦力",
  ["illustrator:efengqi__godlvmeng"] = "卡卡也有",

  ["efengqi__gongxin"] = "攻心",
  ["#efengqi__gongxin_delay"] = "攻心",
  [":efengqi__gongxin"] = "摸牌阶段开始时，你可以观看一名角色的手牌并选择一项:将其中一张<font color='red'>♥</font>牌置于牌堆顶；此阶段结束时再发动一次。",
  ["#efengqi__gongxin-ivk"] = "攻心：可以观看一名角色的手牌",
  ["#efengqi__gongxin-place"] = "置于牌堆顶",
  ["#efengqi__gongxin-cancel"] = "再发动",

  ["efengqi__shelie"] = "涉猎",
  ["#efengqi__shelie_delay"] = "涉猎",
  [":efengqi__shelie"] = "出牌阶段开始时，你可以亮出牌堆顶五张牌并选择一项：获得其中每种花色的牌各一张；此回合结束时再发动一次。",
  ["#efengqi__shelie-obtain"] = "获得其中每种花色的牌各一张",
  ["#efengqi__shelie-cancel"] = "此回合结束时再发动一次",
}

return extension
